/*
 * Created on Sun Sep 17 2017
 * 
 * Copyright (c) 2017 jianglinjie
 */
'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _ResponseType = require('../constants/ResponseType');

var ResponseType = _interopRequireWildcard(_ResponseType);

var _NetworkReponse = require('./NetworkReponse');

var _NetworkReponse2 = _interopRequireDefault(_NetworkReponse);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var FetchStrategy = function () {
  function FetchStrategy() {
    _classCallCheck(this, FetchStrategy);
  }

  _createClass(FetchStrategy, [{
    key: 'start',
    value: function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(request) {
        var fetchRequest, response, body;
        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                fetchRequest = this.constructFetchRequest(request);
                _context.next = 3;
                return fetch(fetchRequest);

              case 3:
                response = _context.sent;
                body = undefined;
                _context.t0 = request.responseType;
                _context.next = _context.t0 === ResponseType.ARRAY_BUFFER_TYPE ? 8 : _context.t0 === ResponseType.BLOB_TYPE ? 12 : _context.t0 === ResponseType.JSON_TYPE ? 16 : _context.t0 === ResponseType.TEXT_TYPE ? 20 : _context.t0 === ResponseType.FORM_DATA_TYPE ? 24 : 28;
                break;

              case 8:
                _context.next = 10;
                return response.arrayBuffer();

              case 10:
                body = _context.sent;
                return _context.abrupt('break', 29);

              case 12:
                _context.next = 14;
                return response.blob();

              case 14:
                body = _context.sent;
                return _context.abrupt('break', 29);

              case 16:
                _context.next = 18;
                return response.json();

              case 18:
                body = _context.sent;
                return _context.abrupt('break', 29);

              case 20:
                _context.next = 22;
                return response.text();

              case 22:
                body = _context.sent;
                return _context.abrupt('break', 29);

              case 24:
                _context.next = 26;
                return response.formData();

              case 26:
                body = _context.sent;
                return _context.abrupt('break', 29);

              case 28:
                return _context.abrupt('break', 29);

              case 29:
                return _context.abrupt('return', new _NetworkReponse2.default(response.status, body));

              case 30:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function start(_x) {
        return _ref.apply(this, arguments);
      }

      return start;
    }()
  }, {
    key: 'constructFetchRequest',
    value: function constructFetchRequest(request) {
      var requestInit = { method: request.method };

      if (request.headers && Object.keys(request.headers).length) {
        requestInit.headers = new Headers(request.headers);
      } else {
        requestInit.headers = new Headers();
      }

      if (request.body) {
        requestInit.body = request.body;
      }

      requestInit.mode = 'cors';

      var fetchRequest = new Request(request.requestUrl, requestInit);

      return fetchRequest;
    }
  }]);

  return FetchStrategy;
}();

exports.default = FetchStrategy;